#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<unordered_map>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef int LL;
typedef pair<LL,LL>P;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
int main(void)
{
  LL n;n=read();
  LL ans=0;
  bool flag=1;
  for(LL i=1;i<=n;i++){
      LL x,y;x=read();y=read();
      if(x<y) swap(x,y);
      if(x==y){
        continue;
      }
      if(flag==0) continue;
      map<LL,LL>map1;
      queue<pair<LL,LL>>que;
      que.push({x,0});
      while(!que.empty()){
           P now=que.front();que.pop();
           if(!map1.count(now.first)) map1[now.first]=now.second;
           else map1[now.first]=min(map1[now.first],now.second);
           if(now.first%3==0){
              que.push({now.first/3,now.second+1});
           }
           if(now.first%5==0){
              que.push({now.first/5*3,now.second+1});
           }
      }
      que.push({y,0});
      LL res=0x3f3f3f3f;
      while(!que.empty()){
        P now=que.front();que.pop();
        if(map1.count(now.first)){
            res=min(res,now.second+map1[now.first]);
        }
        if(now.first%3==0){
            que.push({now.first/3,now.second+1});
        }
        if(now.first%5==0){
              que.push({now.first/5*3,now.second+1});
        }
      }
      if(res==0x3f3f3f3f){
        flag=0;
        continue;
      }
      else ans+=res;
  }
  if(flag) printf("%d\n",ans);
  else if(flag==0) printf("-1\n");
return 0;
}
